﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using LotteryAdmin.AdminService;
using System.Windows.Data;

namespace LotteryAdmin
{
    public partial class AdminProfitLoss : UserControl
    {
        LotteryAdminClient srv;
        DateTime startDate
        {
            get { return datePickerStart.SelectedDate.Value.Date; }
            set { datePickerStart.SelectedDate = value; }
        }
        DateTime endDate
        {
            get { return datePickerEnd.SelectedDate.Value.Date.AddDays(1).AddSeconds(-1); }
            set { datePickerEnd.SelectedDate = value; }
        }
        public AdminProfitLoss()
        {
            InitializeComponent();
            this.datePickerStart.SelectedDate = DateTime.Now;
            this.datePickerEnd.SelectedDate = DateTime.Now;
            this.Loaded += new RoutedEventHandler(AdminProfitLoss_Loaded);
            this.KeyDown += new KeyEventHandler(Page_KeyDown);
        }
        void Page_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.PlatformKeyCode == 13)
                this.btnSearch_Click(this.btnSearch, null);
        }

        void AdminProfitLoss_Loaded(object sender, RoutedEventArgs e)
        {
            LoadForm();
        }
        
        void LoadForm()
        {
            this.sp_Type.Visibility = Visibility.Collapsed;
            srv = WcfProxy.GetProxy;
            srv.GetAdminProfitReportAsync(startDate, endDate);
            loadingProgress.IsBusy = true;
            srv.GetAdminProfitReportCompleted += (s, e) =>
            {
                loadingProgress.IsBusy = false;
                this.dataForm1.CurrentItem = e.Result.FirstOrDefault();
                if (e.Result.Count == 0)
                    this.dataForm1.CurrentItem = new ProfitReport();
            };
        }
        void LoadGrid()
        {
            this.sp_Type.Visibility = Visibility.Visible;
            string type = (cbxType.SelectedItem as ComboBoxItem).Content.ToString() == "请选择" ? "":
                ((cbxType.SelectedItem as ComboBoxItem).Content.ToString()=="派奖"?"盈亏":
                (cbxType.SelectedItem as ComboBoxItem).Content.ToString());
            srv = WcfProxy.GetProxy;
            srv.GetAdminProfitDetailAsync(type,startDate, endDate);
            loadingProgress.IsBusy = true;
            srv.GetAdminProfitDetailCompleted += (s, e) =>
            {
                loadingProgress.IsBusy = false;
                if (e.Result.Count == 0)
                {
                    MessageBox.Show("没有找到匹配的记录!");
                }
                PagedCollectionView pcv = new PagedCollectionView(e.Result);
                this.dataPager.Source = pcv;
                this.dgList.ItemsSource = pcv;
            };
        }
        void LoadData()
        {
            this.UpdateLayout();
            if (tabItem0!= null && tabItem0.IsSelected)
                LoadForm();
            if (tabItem1 != null && tabItem1.IsSelected)
                LoadGrid();
        }
        private void btnSearch_Click(object sender, RoutedEventArgs e)
        {
            LoadData();
        }

        private void TabControl_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            LoadData();
        }

        private void dgList_LoadingRow(object sender, DataGridRowEventArgs e)
        {
            (this.dgList.Columns[0].GetCellContent(e.Row) as TextBlock).Text = (dataPager.PageCount * dataPager.PageIndex+e.Row.GetIndex() + 1).ToString();
            if((e.Row.DataContext as ProfitDetail).ProfitLossType=="盈亏")
                (e.Row.DataContext as ProfitDetail).ProfitLossType = "派奖";
        }
    }
}
